<?php
namespace App\HttpController\Admin;

use App\Repositories\Contracts\BaseOperateRepository;
use App\Repositories\Eloquent\Admin\MemberInfoModel;
use App\Repositories\Eloquent\Admin\MemberModel;
use EasySwoole\Template\Render;

class Member extends Sys implements BaseOperateRepository {

    private  $_model;
    private  $_model_info;

    public function index()
    {
        $this->show('admin/member/index');
    }

    public function getList()
    {
        $this->_model = new MemberModel();

        $where      = [];
        if(!empty($this->_params['key'])){
            $map    = $this->_params['key'];
            $map['id'] ? $where['a.id'] = $map['id'] : null;
            $map['type'] ? $where['a.type'] = $map['type'] : null;
            $map['name'] ? $where['b.name'] = ['%' . $map['name'] . '%', 'like'] : null;
        }
        $where['a.status'] = [[0,1],'in'];
        $where['a.deleted'] = 0;
        $datas  = $this->_model->getList($where);

        $this->ajaxSuccess($datas['datas'],'',$datas['count']);


        // TODO: Implement getList() method.
    }

    public function add()
    {
        $this->show('admin/member/add');
    }

    public function add_ajax()
    {
        $this->_model = new MemberModel();
        //验证帐号
        $check = $this->_model->getItemByName($this->_params['name']);
        if ($check) {
           $this->ajaxError(0,"名称");
        }
        $item = $this->_params;
        $item['status'] = 1;
        $reslut = $this->_model->addItem($item);
        if ($reslut) {
            $this->ajaxSuccess();
            return;
        }
        $this->ajaxError();

    }

    public function edit()
    {
        $this->_model = new MemberModel();
        $param = $this->_params;
        $id = 0;
        if (!empty($param['id'])) {
            $id = $param['id'];
        } else {
           $this->ajaxError(0,"参数错误");
        }
        $item = [];
        if ($id > 0) {
            $tmp['id'] = $id;
            $item = $this->_model->getItem($id);
            $item['expiration_time'] = date('Y-m-d H:i:s',$item['expiration_time']);
            $item['last_login_time'] = date('Y-m-d H:i:s',$item['last_login_time']);
            $item['add_time'] = date('Y-m-d H:i:s',$item['add_time']);
        }
        $this->show('admin/member/edit',['item'=>$item]);

    }

    public function edit_ajax()
    {
        $this->_model_info = new MemberInfoModel();
        $id = $this->_params['id'];
        if ($id > 0) {
            unset($this->_params['id']);
            if(!empty($this->_params['expiration_time'])){
                $this->_params['expiration_time'] = strtotime($this->_params['expiration_time']);
            }
            $item = $this->_params;
            $reslut = $this->_model_info->updateItem($item,['id_user'=>$id,]);

            if ($reslut) {
                $this->ajaxSuccess();
            }
            $this->ajaxError(0,"参数错误");
        }
    }

    public function status()
    {
        $this->_model = new MemberModel();
        $param = $this->_params;
        if (!empty($param['ids']) && !is_null($param['status'])) {
            $ids = $param['ids'];
            //-1 删除 0禁用 1 正常启用
            $status = $param['status'];
        } else {
            $this->ajaxError(0,"参数错误");
        }
        if ($status < -2 && $status > 1) {
            $this->ajaxError(0,"参数错误");
        }
        if (!$ids) {
            $id = I('id');
            if ($id) {
                $ids [] = $id;
            }
        }
        if (!$ids) {
            $this->ajaxError(0,"您未选择任何记录");
        }
        $ids = explode(',', $ids);
        $data['status'] = $status;
        $result = $this->_model->status($ids, $data);
        $result? $this->ajaxSuccess():$this->ajaxError(0,"操作失败");
    }

    public function del()
    {
        $this->_model = new MemberModel();
        $id = $this->_params['id'];
        if (empty($id)) {
            $this->ajaxError(0,"参数错误");
        }
        $result = $this->_model->delById($id);
        $result? $this->ajaxSuccess():$this->ajaxError(0,"操作失败");

    }

    public function sort()
    {
        // TODO: Implement px() method.
    }


    function reload(){
        Render::getInstance()->restartWorker();
        $this->response()->write(1);
    }

    protected function gc()
    {
        parent::gc(); // TODO: Change the autogenerated stub
        $this->_model = null;
        $this->_model_info = null;
    }


}
